<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px;width:100%"
  >
    <el-col>
      <strong v-if="isCompany">{{ treeNode.name }}</strong>
      <span v-else>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row
        type="flex"
        justify="end"
      >
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown trigger="click">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="!isCompany" @click.native="addFn">添加子部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isCompany"
                @click.native="editFn"
              >查看部门</el-dropdown-item>
              <el-dropdown-item
                v-if="!isCompany"
                @click.native="delFn"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
    <Dialog
      ref="dialog"
      :company-id="treeNode.id"
      :title="title"
      :modal-append-to-body="false"
    />
  </el-row>
</template>

<script>
import Dialog from './dialog'
import { delDepartment } from '@/api/company'
// 该组件需要对外开放属性 外部需要提供一个对象 对象里需要有name  manager
export default {
  name: 'TreeTools',
  components: { Dialog },
  // props可以用数组来接收数据 也可以用对象来接收
  // props: {   props属性: {  配置选项 }  }
  props: {
    //   定义一个props属性
    treeNode: {
      type: Object, // 对象类型
      required: true // 要求对方使用您的组件的时候 必须传treeNode属性 如果不传 就会报错
    },
    isCompany: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: ''
    }
  },
  methods: {
    editFn() {
      this.title = '编辑部门'
      this.$refs.dialog.form = this.treeNode
      this.$refs.dialog.dialogFormVisible = true
    },
    addFn() {
      this.title = '新增部门'
      // pid用于表示上一级数据
      this.$refs.dialog.form.pid = this.treeNode.id
      this.$refs.dialog.dialogFormVisible = true
    },
    async delFn() {
      // 二次询问
      await this.$confirm('确定删除吗?')
      // 发送请求
      await delDepartment(this.treeNode.id)
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      //   通过子传父通信，更新页面数据
      this.$emit('delDepartment')
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog .el-dialog__header {
  background-color: rgb(100, 159, 222);
  span {
    color: #fff;
  }
}
</style>
